import express from "express"
import bodyParser from "body-parser"
import cookieParser from "cookie-parser"
import cookieSession from "cookie-session"
import multer from "multer";

//创建express的实例
const server=express();

//配置中间件
server.use(bodyParser.urlencoded({
    limit:"100mb",
    extended:false
}))
server.use(bodyParser.json())

server.use(cookieParser("userker"))
server.use(cookieSession({
    name:"userker",
    keys:["aaa","bbb","ccc"],
    maxAge:1000*60*20
}))
const upload=multer({
    dest:"./../src/upload"
})

server.use(upload.any())
server.use("*",(request,response,next)=>{

    request.header("Content-Type","application/json;charset=utf-8");
    
    if (request.originalUrl.indexOf("/upload") != -1 ||
        request.originalUrl.indexOf("/js") != -1 ||
        request.originalUrl.indexOf("/index") != -1 ||
        request.originalUrl.indexOf("/css") != -1 ||
        request.originalUrl.indexOf("/images") != -1 ||
        request.originalUrl.indexOf("/product") != -1 ||
        request.originalUrl.indexOf("/login") != -1 ||
        request.originalUrl.indexOf("/register") != -1) {
        next();
    // } else {
    //     console.log(request.session["users"])
    //     if (request.session["users"] == null || request.session["users"] == undefined) {
    //         response.json({
    //             msg: "请先登录",
    //             status: -1
    //         })
    //         // 服务器端的重定向 response.redirect("./login.html"); 不能用ajax的请求
    //     } else {
    //         next();
    //     }
    // }
    next()
})
//配置静态资源服务器
server.use(express.static("./../src"))
//挂载api路由对象(保安大爷)
server.use("/api",require("./routes/ApiRoute").apiRouter);


server.listen(8081,()=>{
    console.log("服务器启动完毕.......")
})